#include "protol_iot.h"
#include "drv_iot.h"

#include "SMProcess.h"
#include "drv_rgb.h"

#include "stdio.h"

#define IOT_PACK_BUF_SIZE 50
static uint8_t pack_buf[IOT_PACK_BUF_SIZE] = {0};
static uint8_t buf_index = 0;


uint8_t wifi_state = 0;
IOT_TIME iot_time = {0};
uint8_t iot_wifi_signal = 0;
uint8_t iot_wifi_mac[6] = {0};

extern uint32_t xdata eventValue;

uint16_t xdata IotPm2d5Value = 0;
uint16_t xdata IotDeviceMode = 0;
uint16_t xdata IOTFanLevel = 0;
uint16_t xdata IOTKillState = 0;

bool iot_data_updata_flag = FALSE;
bool iot_state_updata_flag = FALSE;
bool iot_abnor_updata_flag = FALSE;

uint8_t iot_request_flag = 0x00;


uint8_t iot_device_data_pack(void)
{
	buf_index = 0;
	
	pack_buf[buf_index++] = CMOMMAND_DEVICE_DATA;
	pack_buf[buf_index++] = DEVICE_TYPE;
	pack_buf[buf_index++] = DEVICE_MODEL;
	
	pack_buf[buf_index++] = IotPm2d5Value%256;//pm2.5
	pack_buf[buf_index++] = IotPm2d5Value/256;
	
	pack_buf[buf_index++] = 0;//tvoc
	pack_buf[buf_index++] = 0;
	
	pack_buf[buf_index++] = 0;//temperature
	pack_buf[buf_index++] = 0;
	
	pack_buf[buf_index++] = 0;//humity
	pack_buf[buf_index++] = 0;
	
	pack_buf[buf_index++] = 0;//co2
	pack_buf[buf_index++] = 0;
	
	pack_buf[buf_index++] = 0;//pm10
	pack_buf[buf_index++] = 0;
	
	pack_buf[buf_index++] = 0;//chco
	pack_buf[buf_index++] = 0;
	
	iot_protol_frame_send(pack_buf, buf_index);
	
	return 0;
	
}

uint8_t iot_device_state_pack(void)
{
	buf_index = 0;
	pack_buf[buf_index++] = CMOMMAND_DEVICE_STATE;
	pack_buf[buf_index++] = DEVICE_TYPE;
	pack_buf[buf_index++] = DEVICE_MODEL;
	
	pack_buf[buf_index++] = IotDeviceMode;//mode
	
	pack_buf[buf_index++] = IOTFanLevel;//fan_level
	
	pack_buf[buf_index++] = 0;//motor speed
	pack_buf[buf_index++] = 0;
	
	pack_buf[buf_index++] = 0;//power off time
	pack_buf[buf_index++] = 0;
	
	pack_buf[buf_index++] = IOTKillState;//disindefect state
	
	pack_buf[buf_index++] = 0;//display light percent
	
	pack_buf[buf_index++] = 0;//screen life A percent
	pack_buf[buf_index++] = 0;//screen life B percent
	
	iot_protol_frame_send(pack_buf, buf_index);
	
	return 0;
}

uint8_t iot_device_control_pack(void)
{
	buf_index = 0;
	
	pack_buf[buf_index++] = CMOMMAND_DEVICE_CONTROL;
	pack_buf[buf_index++] = 0;//reply operate result
	
	iot_protol_frame_send(pack_buf, buf_index);
	
	return 0;
}

uint8_t iot_device_abnor_pack(void)
{
	buf_index = 0;
	pack_buf[buf_index++] = CMOMMAND_DEVICE_DATA;
	pack_buf[buf_index++] = DEVICE_TYPE;
	pack_buf[buf_index++] = DEVICE_MODEL;
	
	pack_buf[buf_index++] = 0;//motor abnor
	
	pack_buf[buf_index++] = 0;//disindefect abnor
	
	pack_buf[buf_index++] = 0;//sensor
	
	pack_buf[buf_index++] = 0;//screen expire
	
	iot_protol_frame_send(pack_buf, buf_index);
	
	return 0;
}


uint8_t iot_sys_device_request_pack(uint8_t buf)
{
	buf_index = 0;
	
	pack_buf[buf_index++] = COMMAND_SYS_DEVICE_REQUEST;
	pack_buf[buf_index++] = 0;//return the same request code
	
	iot_protol_frame_send(pack_buf, buf_index);
	
	return 0;
}

uint8_t iot_sys_device_sn_pack(void)
{
	buf_index = 0;
	
	pack_buf[buf_index++] = COMMAND_SYS_DEVICE_SN;
	pack_buf[buf_index++] = DEVICE_TYPE;
	pack_buf[buf_index++] = DEVICE_MODEL;
	
	pack_buf[buf_index++] = 'F';
	pack_buf[buf_index++] = 'J';
	pack_buf[buf_index++] = '-';
	pack_buf[buf_index++] = 'F';
	pack_buf[buf_index++] = '2';
	pack_buf[buf_index++] = '0';
	pack_buf[buf_index++] = '-';
	pack_buf[buf_index++] = '1';
	pack_buf[buf_index++] = '2';
	pack_buf[buf_index++] = '3';
	pack_buf[buf_index++] = '4';
	pack_buf[buf_index++] = '5';
	
	iot_protol_frame_send(pack_buf, buf_index);
	
	return 0;
}

uint8_t iot_sys_wifi_state_pack(void)
{
	buf_index = 0;
	pack_buf[buf_index++] = COMMAND_SYS_WIFI_STATE;
	
	iot_protol_frame_send(pack_buf, buf_index);
	
	return 0;
}

uint8_t iot_sys_local_time_pack(void)
{
	buf_index = 0;
	pack_buf[buf_index++] = COMMAND_SYS_LOCAL_TIME;
	
	iot_protol_frame_send(pack_buf, buf_index);
	
	return 0;
}

uint8_t iot_sys_wifi_signal_pack(void)
{
	buf_index = 0;
	pack_buf[buf_index++] = COMMAND_SYS_WIFI_SIGNAL;
	
	iot_protol_frame_send(pack_buf, buf_index);
	
	return 0;
}

uint8_t iot_sys_wifi_mac_pack(void)
{
	buf_index = 0;
	pack_buf[buf_index++] = COMMAND_SYS_WIFI_MAC;
	
	iot_protol_frame_send(pack_buf, buf_index);
	
	return 0;
}

static uint8_t iot_device_data_unpack(char* buf)
{
	printf("iot_device_data_unpack\n");
	
	return 0;
}

static uint8_t iot_device_state_unpack(char* buf)
{
	printf("iot_device_state_unpack\n");
	
	return 0;
}

static uint8_t iot_device_control_unpack(char* buf)
{
	switch(buf[3])
	{
		case 0x01:
		{
			if(buf[4] == 0x00){
				if(IotDeviceMode != 0)
					eventValue = CTR_ONOFF_EVENT;
			}else
			if(buf[4] == 0x01){
				if(IotDeviceMode == 0)
					eventValue = CTR_ONOFF_EVENT;				
			}else
			if(buf[4] == 0x02){
					eventValue = CTR_ONOFF_EVENT;					
			}
		}
		break;
		
		case 0x02:
		{
			if(buf[4] == 0x01){
				if(IotDeviceMode != 0x01)
					eventValue = CTR_MODE_SLEEP_EVENT;
			}else
			if(buf[4] == 0x02){
				if(IotDeviceMode != 0x02)
					eventValue = CTR_MODE_AUTO_EVENT;				
			}else
			if(buf[4] == 0x03){
				if(IotDeviceMode != 0x03)
					eventValue = CTR_MODE_NORMAL_EVENT;				
			}
		}
		break;
		
		case 0x04:
			if(buf[4] == 0x00){
				if(disinfect_status_get())
					eventValue = CTR_AMD_EVENT;
			}else
			if(buf[4] == 0x01){
				if(disinfect_status_get() == 0)
					eventValue = CTR_AMD_EVENT;
			}
			break;
		case 0x05:
			if(buf[4] == 0x00){
				if(ion_status_get())
					eventValue = CTR_ION_EVENT;
			}else
			if(buf[4] == 0x01){
				if(ion_status_get() == 0)
					eventValue = CTR_ION_EVENT;
			}
			break;
		case 0x07:
			if(buf[4] != IOTFanLevel)
			{
				IOTFanLevel = buf[4];
				eventValue = CTR_MODE_NORMAL_EVENT;
			}
			break;
			
		case 0x10:
			if(buf[4] == 0x01)
			{
				iot_data_updata_flag = TRUE;
			}else
			if(buf[4] == 0x02)
			{
				iot_state_updata_flag = TRUE;
			}
			break;
	}
	
	
	
	printf("iot_device_control_unpack\n");
	iot_device_control_pack();
	
	return 0;
}

static uint8_t iot_device_abnor_unpack(char* buf)
{
	printf("iot_device_state_unpack\n");
	
	return 0;
}


static uint8_t iot_sys_device_request_unpack(char* buf)
{
	printf("iot_sys_device_request_unpack\n");
	iot_sys_device_request_pack(buf[3]);
	
	return 0;
}

static uint8_t iot_sys_device_sn_unpack(char* buf)
{
	printf("iot_sys_device_sn_unpack\n");
	
	return 0;
}

static uint8_t iot_sys_wifi_state_unpack(char* buf)
{
	wifi_state = buf[3];
	printf("iot_sys_wifi_state_unpack, state:%bu\n", wifi_state);
	iot_sys_wifi_state_pack();
	
	return 0;
}

static uint8_t iot_sys_local_time_unpack(char* buf)
{
	iot_time.year   = buf[3];
	iot_time.month  = buf[4];
	iot_time.day    = buf[5];
	iot_time.hour   = buf[6];
	iot_time.minute = buf[7];
	
	printf("iot_sys_local_time_unpack, y:%bu m:%bu d:%bu h:%bu m:%bu\n", iot_time.year, iot_time.month, iot_time.day, iot_time.hour, iot_time.minute);
	iot_sys_local_time_pack();
	
	return 0;
}

static uint8_t iot_sys_wifi_signal_unpack(char* buf)
{
	iot_wifi_signal = buf[3];
	printf("iot_sys_wifi_signal_unpack, state:%bu\n", iot_wifi_signal);
	iot_sys_wifi_signal_pack();
	
	return 0;
}

static uint8_t iot_sys_wifi_mac_unpack(char* buf)
{
	iot_wifi_mac[0] = buf[3];
	iot_wifi_mac[1] = buf[3];
	iot_wifi_mac[2] = buf[3];
	iot_wifi_mac[3] = buf[3];
	iot_wifi_mac[4] = buf[3];
	iot_wifi_mac[5] = buf[3];
	
	printf("iot_sys_wifi_mac_unpack, mac %bu:%bu:%bu:%bu:%bu:%bu\n", iot_wifi_mac[0], iot_wifi_mac[1], iot_wifi_mac[2], iot_wifi_mac[3], iot_wifi_mac[4], iot_wifi_mac[5]);
	iot_sys_wifi_mac_pack();
	
	return 0;
	
}



uint8_t iot_protol_parse(char* Buf)
{
	uint8_t result = 0;
	switch(Buf[2])
	{
		case CMOMMAND_DEVICE_DATA:
			iot_device_data_unpack(Buf);
			break;
		case CMOMMAND_DEVICE_STATE:
			iot_device_state_unpack(Buf);
			break;
		case CMOMMAND_DEVICE_CONTROL:
			iot_device_control_unpack(Buf);
			break;
		case CMOMMAND_DEVICE_ABNOR:
			iot_device_abnor_unpack(Buf);
			break;
		
		case COMMAND_SYS_DEVICE_REQUEST:
			iot_sys_device_request_unpack(Buf);
			break;
		case COMMAND_SYS_DEVICE_SN:
			iot_sys_device_sn_unpack(Buf);
			break;
		case COMMAND_SYS_WIFI_STATE:
			iot_sys_wifi_state_unpack(Buf);
			break;
		case COMMAND_SYS_LOCAL_TIME:
			iot_sys_local_time_unpack(Buf);
			break;
		case COMMAND_SYS_WIFI_SIGNAL:
			iot_sys_wifi_signal_unpack(Buf);
			break;
		case COMMAND_SYS_WIFI_MAC:
			iot_sys_wifi_mac_unpack(Buf);
			break;
		
	}
	
	return result;
}

uint8_t iot_protol_send(void)
{
	//if(wifi_state == 0x03)
	if(1)
	{
		if(iot_data_updata_flag)
		{
			iot_data_updata_flag = FALSE;
			
			iot_device_data_pack();
		}
		
		if(iot_state_updata_flag)
		{
			iot_state_updata_flag = FALSE;
			
			iot_device_state_pack();
		}

		if(iot_abnor_updata_flag)
		{
			iot_abnor_updata_flag = FALSE;
			
			iot_device_abnor_pack();
		}	
	}
	
	if(iot_request_flag)
	{	
		iot_sys_device_request_pack(iot_request_flag);
		iot_request_flag = 0;
	}
}

